<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="石号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="codeNum">
              <j-popup
                v-model="model.codeNum"
                field="codeNum"
                org-fields="stone_code_num,cate_id,shape,name,level,cutting,color,size_num,weight,price,count_price_pattern"
                dest-fields="codeNum,cateName,shape,name,level,cutting,color,sizeNum,unitWeight,stoneUnitPrice,countPricePattern"
                code="stone_style_report"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="石号名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入石号名称" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="石类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cateName">
              <j-dict-select-tag type="list" v-model="model.cateName" dictCode="stone_category,cate_name,id" placeholder="请选择石类" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="形状" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shape">
              <j-dict-select-tag type="list" v-model="model.shape" dictCode="shape" placeholder="请选择形状" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="筛号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sizeNum">
              <a-input v-model="model.sizeNum"placeholder="请输入筛号" readOnly></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="镶法" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inlay">
              <j-dict-select-tag type="list" v-model="model.inlay" dictCode="inlay_type" placeholder="请选择镶法" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="石数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneCount">
              <a-input v-model="model.stoneCount" placeholder="请输入石数" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="单粒石重(ct)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitWeight">
              <a-input v-model="model.unitWeight"  placeholder="请输入单粒石重(ct)" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="石重(ct)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="weight">
              <a-input v-model="model.weight" placeholder="单粒石重(ct)*石数" readOnly></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="镶工单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inlayUnitCost">
              <a-input v-model="model.inlayUnitCost" placeholder="请输入镶工单价" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="计价方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="countPricePattern">
              <j-dict-select-tag type="list"  v-model="model.countPricePattern" dictCode="count_price_pattern" placeholder="请选择计价方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="镶工总价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inlayTotalCost">
              <a-input v-model="model.inlayTotalCost" placeholder="请输入镶工总价" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="石单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneUnitPrice">
              <a-input v-model="model.stoneUnitPrice" placeholder="请输入石单价"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="石总价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stoneTotalPrice">
              <a-input v-model="model.stoneTotalPrice" placeholder="请输入石总价" readOnly></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="color">
              <j-dict-select-tag type="list" v-model="model.color" dictCode="color" placeholder="请选择颜色" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="车工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cutting">
              <j-dict-select-tag type="list" v-model="model.cutting" dictCode="cutting" placeholder="请选择车工" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="配石方案" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="combination">
              <a-input v-model="model.combination" placeholder="请输入配石方案" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="level">
              <j-dict-select-tag type="list" v-model="model.level" dictCode="stone_level" placeholder="请选择级别" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="主副石" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainStone">
              <j-dict-select-tag type="radio" v-model="model.mainStone" dictCode="main_stone_type" placeholder="请选择主副石" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" :sm="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" ></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "StyleStoneInfoModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
          stoneCount: '',
          weight: 0,
          unitWeight: 0,
          countPricePattern: '',
          stoneUnitPrice: 0
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
           codeNum: [
              { required: true, message: '请选择石号!'}
           ],
          stoneCount: [
            { required: true, message: '请输入数量!'}
           ]
        },
        url: {
          add: "/styleInfo/styleInfo/addStyleStoneInfo",
          edit: "/styleInfo/styleInfo/editStyleStoneInfo",
        }

      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    watch : {
      'model.stoneCount' : function() {
        this.setWeight()
      },
      'model.unitWeight' : function() {
        this.setWeight()
      },
      'model.weight' : function() {
        this.setStoneTotalPrice()
      },
      'model.countPricePattern' : function() {
        this.setStoneTotalPrice()
      },
      'model.stoneUnitPrice' : function() {
        this.setStoneTotalPrice()
      },
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if(this.model.unitWeight == null || this.model.unitWeight == ''){
              this.model.unitWeight = 0
            }
            if(this.model.stoneUnitPrice == null || this.model.stoneUnitPrice == ''){
              this.model.stoneUnitPrice = 0
            }
            this.model['styleInfoId'] = this.mainId
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(value,row){
        this.model = Object.assign(this.model, row);
      },
      setWeight(){
        if(this.model.unitWeight != null && this.model.stoneCount != null && this.model.unitWeight !='' && this.model.stoneCount != '') {
          this.model.weight = this.model.unitWeight * this.model.stoneCount
        } else {
          this.model.weight = 0
        }
      },
      setStoneTotalPrice(){
        if(this.model.countPricePattern == 1){//按重
          if(this.model.weight != null && this.model.stoneUnitPrice != null && this.model.weight != '' && this.model.stoneUnitPrice != '') {
            this.model.stoneTotalPrice = parseFloat(this.model.weight * this.model.stoneUnitPrice).toFixed(2)*1;
          } else {
            this.model.stoneTotalPrice = 0
          }
        } else if(this.model.countPricePattern == 2){//按粒
          if(this.model.stoneCount != null && this.model.stoneUnitPrice != null && this.model.stoneCount != '' && this.model.stoneUnitPrice != '') {
            this.model.stoneTotalPrice = parseFloat(this.model.stoneCount * this.model.stoneUnitPrice).toFixed(2)*1;
          } else {
            this.model.stoneTotalPrice = 0
          }
        } else {
          this.model.stoneTotalPrice = 0
        }
      }
    }
  }
</script>
